import { API_CONFIG, getUserAuthHeaders, apiRequest } from '@/config/api'
import type { ApiResponse } from '@/types/assets'

// 体重记录接口
export interface WeightRecord {
  id: number
  weight: string  // API要求字符串类型，如"103.90"
  date: string
  seven_day_average: string  // 7日移动平均，如"0.00"
  created_at: string
  updated_at: string
}

// 体重统计信息接口
export interface WeightStats {
  current_weight: string
  target_weight: string
  weight_diff: string
  bmi: {
    bmi: number
    category: string
  }
}

// 创建体重记录请求数据
export interface CreateWeightRecordData {
  weight: string  // API要求字符串类型，如"103.90"
  date: string
}

// 设置身高与理想体重请求数据
export interface SetHeightAndTargetWeightData {
  height: number
  targetWeight: number
}

class WeightService {
  // 获取体重记录列表
    async getWeightRecords(): Promise<ApiResponse<WeightRecord[]>> {
        try {
            const headers = getUserAuthHeaders()
            const response = await apiRequest<ApiResponse<WeightRecord[]>>(
                API_CONFIG.ENDPOINTS.WEIGHT.RECORDS, 
                { 
                    method: 'GET',
                    headers: headers
                }
            )
            return response
        } catch (error) {
            console.error('getWeightRecords - 请求失败:', error)
            throw error
        }
    }

  // 获取体重统计信息
    async getWeightStats(): Promise<ApiResponse<WeightStats>> {
        const headers = getUserAuthHeaders()
        return await apiRequest<ApiResponse<WeightStats>>(
            API_CONFIG.ENDPOINTS.WEIGHT.STATS, 
            { 
                method: 'GET',
                headers: headers
            }
        )
    }

  // 创建体重记录
  async createWeightRecord(data: CreateWeightRecordData): Promise<ApiResponse<WeightRecord>> {
    const headers = getUserAuthHeaders()
    return await apiRequest<ApiResponse<WeightRecord>>(
      `/weight/records`, 
      {
        method: 'POST',
        headers: headers,
        body: JSON.stringify(data),
      }
    )
  }

  // 设置身高与理想体重
  async setHeightAndTargetWeight(data: SetHeightAndTargetWeightData): Promise<ApiResponse<any>> {
    const headers = getUserAuthHeaders()
    return await apiRequest<any>(
      `/weight/setInfo`, 
      {
        method: 'POST',
        headers: headers,
        body: JSON.stringify(data),
      }
    )
  }
}

export const weightService = new WeightService()